package com.neuedu.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.neuedu.model.User;
import com.neuedu.model.XiaoQu;
import com.neuedu.service.XiaoQuService;
import com.neuedu.service.impl.XiaoQuServiceImpl;
import com.neuedu.util.R;
import com.neuedu.vo.PageVO;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

public class XiaoQuController {
    public static final String FILE_PATH="D:\\teach\\web项目\\exercise20240318物业管理系统\\src\\com\\neuedu\\wuye";
    private XiaoQuService xiaoQuService=new XiaoQuServiceImpl();

    public R page(JSONObject jsonObject){
        Integer pageNo=jsonObject.getInteger("pageNo");
        Integer pageSize=jsonObject.getInteger("pageSize");
        //在某两个日期之间，like 一个给定的小区名称。
        String startTime=jsonObject.getString("startTime");
        String endTime=jsonObject.getString("endTime");
        String name=jsonObject.getString("name");

        PageVO<XiaoQu> pageVO = xiaoQuService.selectOnePage(pageNo, pageSize, startTime, endTime, name);
        return R.ok().setData(pageVO);
    }

    public void showImage(HttpServletRequest request, HttpServletResponse response){
        String fileName = request.getParameter("fileName");
        File file=new File(FILE_PATH+File.separator+fileName);
        try {
            BufferedImage read = ImageIO.read(file);
            ImageIO.write(read,"png",response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public R del(JSONObject jsonObject){
        JSONArray delId = jsonObject.getJSONArray("delId");
        Integer [] deleteId=new Integer[delId.size()];
        for (int i = 0; i < delId.size(); i++) {
            deleteId[i]=delId.getInteger(i);
        }

        int result = xiaoQuService.del(deleteId);
        if(result>0){
            return R.ok("删除成功");
        }else{
            return R.error("删除失败");
        }

    }


    public R add(JSONObject jsonObject){
        Integer userId = jsonObject.getInteger("userId");
        String mingcheng = jsonObject.getString("mingcheng");
        String dizhi = jsonObject.getString("dizhi");
        String mianji = jsonObject.getString("mianji");
        String zongdongshu = jsonObject.getString("zongdongshu");
        String zonghushu = jsonObject.getString("zonghushu");
        String lvhualv = jsonObject.getString("lvhualv");
        String icon = jsonObject.getString("icon");
        String kaifashang = jsonObject.getString("kaifashang");
        String wuye = jsonObject.getString("wuye");
        //String chuangjianshijian = jsonObject.getString("chuangjianshijian");
        String description = jsonObject.getString("description");
        Integer state = jsonObject.getInteger("state");
        XiaoQu xiaoQu=new XiaoQu();
        xiaoQu.setUserid(userId);
        xiaoQu.setMingcheng(mingcheng);
        xiaoQu.setDizhi(dizhi);
        xiaoQu.setMianji(mianji);
        xiaoQu.setZongdongshu(zongdongshu);
        xiaoQu.setZonghushu(zonghushu);
        xiaoQu.setLvhualv(lvhualv);
        xiaoQu.setIcon(icon);
        xiaoQu.setKaifashang(kaifashang);
        xiaoQu.setWuye(wuye);
        //xiaoQu.setChuangjianshijian(chuangjianshijian);
        xiaoQu.setDescription(description);
        xiaoQu.setState(state);

        //调用service，录入一个新的小区
        int result = xiaoQuService.add(xiaoQu);
        if(result>0){
            return R.ok("添加成功");
        }else{
            return R.error("添加失败");
        }

    }


    public R edit(JSONObject jsonObject){
        String s = jsonObject.toJSONString();
        XiaoQu xiaoQu = JSONObject.parseObject(s, XiaoQu.class);
        int result=xiaoQuService.editXiaoQuInfo(xiaoQu);
        if(result>0){
            return R.ok("更新成功");
        }else{
            return R.error("更新失败");
        }
    }

    public R showXiaoQu(JSONObject jsonObject){
        Integer id = jsonObject.getInteger("id");
        XiaoQu xiaoqu = xiaoQuService.findXiaoQuById(id);
        return R.ok().setData(xiaoqu);
    }

    public R selectXiaoQuAll(Map<String,String[]> map){
        List<XiaoQu> xiaoQus = xiaoQuService.selectXiaoQuAll();
        return R.ok().setData(xiaoQus);
    }
}
